﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Security.Policy;
using System.Threading;
using System.Windows.Forms;
using Weapp.Data;

namespace Weapp
{
    public partial class Form1 : Form
    {
        private Url _serverUrl;
        FormSizeCategoryAbm _frmSizeCat;
        private FormConfiguration _frmConfiguration;
        WeappContext _ctx;
        private Registro registrar;

        private AutoResetEvent connectionWaitHandle = new AutoResetEvent(false);

        public Form1()
        {
            InitializeComponent();
            _ctx = new WeappContext();
            //TcpListener is listening on the given port... {
            TcpListener tcpListener = new TcpListener(1234);
            tcpListener.Start();
            Debug.WriteLine("Server Started");
            //Accepts a new connection...
            //Socket socketForClient = tcpListener.AcceptSocket();
            //StreamWriter and StreamReader Classes for reading and writing the data to and fro.
            //The server reads the meassage sent by the Client ,converts it to upper case and sends it back to the client.
            //Lastly close all the streams.
            try
            {
                //if (socketForClient.Connected)
                //{
                //    while (true)
                //    {
                //        Debug.WriteLine("Client connected");
                //        NetworkStream networkStream = new NetworkStream(socketForClient);
                //        StreamWriter streamWriter = new StreamWriter(networkStream);
                //        StreamReader streamReader = new StreamReader(networkStream);
                //        string line = streamReader.ReadLine();
                //        Debug.WriteLine("Read:" + line);
                //        line = line.ToUpper() + "!";
                //        streamWriter.WriteLine(line);
                //        Debug.WriteLine("Wrote:" + line);
                //        streamWriter.Flush();

                        while (true)
                        {
                            IAsyncResult result = tcpListener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
                            connectionWaitHandle.WaitOne(); // Wait until a client has begun handling an event
                            connectionWaitHandle.Reset(); // Reset wait handle or the loop goes as fast as it can (after first request)
                        }
                //    }
                //}
                //socketForClient.Close();
                //Debug.WriteLine("Exiting...");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }

        private void HandleAsyncConnection(IAsyncResult result)
        {
            TcpListener listener = (TcpListener)result.AsyncState;
            TcpClient client = listener.EndAcceptTcpClient(result);
            connectionWaitHandle.Set(); //Inform the main thread this connection is now handled

            //... Use your TcpClient here

            client.Close();
        }



        private void toolStripMenuItemSizeCategories_Click(object sender, EventArgs e)
        {
            _frmSizeCat = new FormSizeCategoryAbm(_ctx);
            _frmSizeCat.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            _frmSizeCat.TopLevel = false;
            _frmSizeCat.Show();

            dockContainer1.Add(_frmSizeCat, Crom.Controls.Docking.zAllowedDock.All, Guid.NewGuid());
        }

        private void configToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _frmConfiguration = new FormConfiguration();
            _frmConfiguration.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            _frmConfiguration.TopLevel = false;
            _frmConfiguration.Show();
            dockContainer1.Add(_frmConfiguration, Crom.Controls.Docking.zAllowedDock.All, Guid.NewGuid());
        }

        private void dockContainer1_Load(object sender, EventArgs e)
        {
            
        }

        private void registrarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            registrar = new Registro();
            registrar.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            registrar.TopLevel = false;
            registrar.Show();

            dockContainer1.Add(registrar, Crom.Controls.Docking.zAllowedDock.All, Guid.NewGuid());

        }
    }
}
